/*
 * ToolTipDemo.java  0.4.0 / Oct 24, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
package com.cute4j.demos.swingset;

import com.cute4j.swing.CheckBox;
import com.cute4j.swing.Component;
import com.cute4j.swing.Container;
import com.cute4j.swing.ImageIcon;
import com.cute4j.swing.Label;
import com.cute4j.swing.ScrollPane;
import com.cute4j.swing.Separator;
import com.cute4j.swing.ToolButton;
import com.cute4j.swing.ToolTip;
import com.cute4j.swing.WhatsThisManager;
import com.cute4j.swing.events.ActionEvent;
import com.cute4j.swing.events.ActionListener;
import com.cute4j.swing.events.SwingEvent;
import com.cute4j.swing.graphics.Color;
import com.cute4j.swing.graphics.Font;
import com.cute4j.swing.layouts.formlayout.CellConstraints;
import com.cute4j.swing.layouts.formlayout.FormLayout;

/**
 * Demonstration of the tooltips.
 *
 * @author Slobodan Vrkacevic
 * @version 0.4.0
 */
public class ToolTipDemo extends DemoModule {

// Public Constructors. --------------------------------------------------------

  /**
   * Constructs new ToolTipDemo object.
   */
  public ToolTipDemo() {
    super("ToolTip Demo", "Demonstration of the tooltips.");
  }

// Overridden Methods. ---------------------------------------------------------

  /**
   * {@inheritDoc}
   */
  protected Component buildDemoComponent() {
    FormLayout layout = new FormLayout("p:g, c:p, 8dlu, p, 4dlu, p, p:g",
                                       "f:p, 4dlu, p");
    Container panel = new Container(layout);
    panel.setWhatsThisText(
            "<p style='white-space:pre'>Demonstration of the <i>tooltips</i> " +
            "based on <br>the silly <b>ToolTip</b> demo from SwingSet2.</p>");
    panel.setBackground(Color.WHITE);
    panel.setInsets(32, 24, 16, 16);
    CellConstraints cc = new CellConstraints();

    panel.add(new ToolTipDemoLabel(), cc.xy(2, 1));
    Label label = new Label("Move and hold mouse pointer over the image.");
    label.setFont(label.getFont().deriveFont(Font.BOLD));
    panel.add(label, cc.xy(2, 3));
    panel.add(new Separator(Separator.VERTICAL), cc.xy(4, 1));

    Container toolsPanel = new Container(new FormLayout("p",
                                                            "p, 4dlu, p"));
    CheckBox toolTipsEnabled = new CheckBox("ToolTips Enabled");
    toolTipsEnabled.setSelected(true);
    toolTipsEnabled.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        ToolTip.setToolTipsEnabled(((CheckBox)e.getSource()).isSelected());
      }
    });
    toolsPanel.add(toolTipsEnabled, cc.xy(1, 1));
    String imageName = "resources/images/tooltip/WhatsThis.png";
    ToolButton whatsThisButton = new ToolButton(
                       new ImageIcon(loadResource(imageName)), "What's This?");
    whatsThisButton.setToolButtonStyle(ToolButton.STYLE_TEXT_BESIDE_ICON);
    whatsThisButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        WhatsThisManager.enterWhatsThisMode();
      }
    });
    toolsPanel.add(whatsThisButton, cc.xy(1, 3));
    panel.add(toolsPanel, cc.xy(6, 1));

    return new ScrollPane(panel);
  }

// Inner Classes. --------------------------------------------------------------

  /**
   * ToolTip Demo Label.
   */
  class ToolTipDemoLabel extends Label {
    private boolean moo;
    private boolean milk;
    private boolean tail;
    public ToolTipDemoLabel() {
      String imageName = "resources/images/tooltip/Cow.png";
      setIcon(new ImageIcon(loadResource(imageName)));
      enableEvents(SwingEvent.LOCATION_TOOL_TIP_MASK);
    }
    public String getToolTipText(int x, int y) {
      if ((x > 30) && (x < 60) && (y > 60) && (y < 85)) {
        if (!moo) {
          moo = true;
          milk = false;
          tail = false;
          return "<html><center><font color=blue size=+2>" +
                 "Mooooooo</font></center></html>";
        }
      } else if ((x > 150) && (x < 260) && (y > 90) && (y < 145)) {
        if (!milk) {
          milk = true;
          moo = false;
          tail = false;
          return "<html><center><font face=AvantGarde size=+1 color=blue>" +
                 "Got Milk?</font></center></html>";
        }
      } else if ((x > 280) && (x < 300) && (y > 20) && (y < 175)) {
        if (!tail) {
          tail = true;
          moo = false;
          milk = false;
          return "<html><em><b>tail.</b></em></html>";
        }
      } else if (moo || milk || tail) {
        moo = false;
        tail = false;
        milk = false;
        return "<html>In <font color=green size=+2>Swing!</font>, " +
        		   "tooltips can use HTML to <ul><li>Have Lists<li><b>Bold</b> " +
        		   "text<li><em>emphasized</em> text<li>text with " +
        		   "<font color=red>Color</font><li>text in different " +
        		   "<font size=+3>sizes</font><li>" +
        		   "<font face=Courier>and Fonts</font></ul>And they can be " +
               "multi-line, too.</html>";
      }
      return null;
    }
  }

// Main Method. ----------------------------------------------------------------

  /**
   * Starts demo as standalone application.
   *
   * @param args the application arguments
   */
  public static void main(String[] args) {
    new ToolTipDemo().show();
  }

}
